package cn.itcast.p2p20.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.IntDef;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.FrameLayout;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import cn.itcast.p2p20.R;

/**
 * Created by Administrator on 2016/7/23.
 * <p/>
 * 状态： state_loading, state_error, state_empty, state_success
 * <p/>
 * state_loading------> loadingView
 * state_error--------> errorView
 * state_empty--------> emptyView
 * state_success------>successView
 * <p/>
 * 状态对应的xml界面需要添加到FrameLayout
 * <p/>
 * 当期显示那一个状态界面： 由当前的状态， 提供设置状态的方法
 * <p/>
 * 外部需要根据状态获取对应的状态界面
 */
public class StateLayout extends FrameLayout {

    public static final int STATE_LOADING = 0;
    public static final int STATE_ERROR = 1;
    public static final int STATE_EMPTY = 2;
    public static final int STATE_SUCCESS = 3;

    @IntDef({STATE_LOADING, STATE_ERROR, STATE_EMPTY, STATE_SUCCESS})
    @Retention(RetentionPolicy.SOURCE)
    public @interface StateEnum {}


    public StateLayout(Context context) {
        this(context, null);
    }

    public StateLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        addStateLayout(STATE_LOADING, R.layout.layout_loding);
//        addStateLayout(STATE_ERROR, R.layout.layout_error);
//        addStateLayout(STATE_EMPTY, R.layout.layout_empty);

        TypedArray typedArrayStateAttrs = context.obtainStyledAttributes(attrs, R.styleable.statelayout_state_attrs);
        int indexCount = typedArrayStateAttrs.getIndexCount();
        for (int i = 0; i < indexCount; i++) {

            int stateValue = typedArrayStateAttrs.getInt(i, -1);
            addStateLayout(stateValue);
        }

        typedArrayStateAttrs.recycle();


        TypedArray typedArraySuccessLayout = context.obtainStyledAttributes(attrs, R.styleable.statelayout_success_layout_attrs);
        int successLayoutResourceId = typedArraySuccessLayout.getResourceId(0, -1);
        if (successLayoutResourceId != -1) {

            addStateLayout(STATE_SUCCESS, successLayoutResourceId);
        }
        typedArraySuccessLayout.recycle();
    }

    //根据xml配置的状态值来添加状态布局
    private void addStateLayout(int stateValue) {

        switch (stateValue) {

            case STATE_LOADING:
                addStateLayout(STATE_LOADING, R.layout.layout_loding);
                break;
            case STATE_ERROR:
                addStateLayout(STATE_ERROR, R.layout.layout_error);
                break;
            case STATE_EMPTY:
                addStateLayout(STATE_EMPTY, R.layout.layout_empty);
                break;
            default:
                break;
        }

    }

    public void addStateLayout(@StateEnum int state,@LayoutRes int stateLayoutRes) {

        addStateView(state, View.inflate(getContext(), stateLayoutRes, null));
    }

    //状态和状态界面的对应
    private SparseArray<View> mStateViews = new SparseArray<>();

    //添加到FrameLayout
    public void addStateView(@StateEnum int state,@NonNull View stateView) {

        View preStatView = mStateViews.get(state);
        if (preStatView != null) {

            removeView(preStatView);
        }

        mStateViews.put(state, stateView);
        addView(stateView);

        //添加状态了是否显示由当前的状态决定
        stateView.setVisibility(state == mCurrentState ? VISIBLE : INVISIBLE);
    }

    private int mCurrentState = STATE_LOADING;

    //state_loading------>state_error---->state_success
    //根据状态显示状态界面
    public void showStateView(@StateEnum int state) {

        //显示一个状态的时候需要把之前的状态隐藏
        View preStateView = mStateViews.get(mCurrentState);
        if (preStateView != null) {

            preStateView.setVisibility(INVISIBLE);
        }

        //显示当前的状态
        View stateView = mStateViews.get(state);
        if (stateView != null) {

            stateView.setVisibility(VISIBLE);
        }

        mCurrentState = state;
    }

    //根据状态获取对应的状态界面
    public View getStateView(@StateEnum int state) {

        return mStateViews.get(state);
    }


}
